
/* makepro2.c by bill buckels 1991 */
/* makes a prodos sys program with an embedded graphic */


#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>

int picfile=0;
int ribfile=0;
char *far screenbuffer;
int ribsize;

/* loads a lib */
int riblode(name)
char *name;
{
   int fh;
   fh = open(name,O_RDONLY|O_BINARY); /* open a binary file */
   ribsize = read(fh,(char *)&screenbuffer[0x2000],0x2000);
   close(fh);
   return ribsize;
}



int bitcopy(char *name1, char *name2)
{
    FILE *fp,*fp2;
    unsigned char c,d;

    long target,count=0;
    int piccount=0,ribcount=0;

    if((fp=fopen(name1,"rb"))==NULL){
                                     perror(name1);
                                     return -1;
                                     }
    fp2=fopen(name2,"wb");
    target=filelength(fileno(fp));

    while(count<target)
        {
        c=fgetc(fp);
        if(count>3)
        {
            if(count>0x2003 && (picfile!=0))
            {
             if(piccount<0x2000)c=screenbuffer[piccount];
             if(piccount>=0x2000 && ribfile!=0 && ribcount<ribsize)
             {
              c=screenbuffer[piccount];
              ribcount++;
             }
             piccount++;
            }
           fputc(c,fp2);

        }
        count++;
        }
    fclose(fp);
    fclose(fp2);

   return(0);
}


main(int argc, char *argv[])
{
    FILE *picture;
    char buf[66],buf2[66];

    if(argc==2 || argc ==3 || argc==4)
    {
    sprintf(buf,"%s.SYS",argv[1]);

    if(argc>2)
    {
     if((picture = fopen(argv[2],"rb"))!=NULL)
     {
        picfile++;
        fclose(picture);
        screenbuffer = _fmalloc(0x4000);
        memset(screenbuffer,0,0x4000);
        strcpy(buf2,argv[2]);
        preparepic(buf2);
      }
    }
    if(argc>3 && picfile!=0)
        ribfile = riblode(argv[3]);

    bitcopy(argv[1],buf);
    if(picfile)_ffree(screenbuffer);

    }
    else
    {
        printf(
        "Usage is [MANX C65 EXECUTABLE PRODOS SYS FILE- NO EXTENSION]\n");
        printf("Second Argument is Picture File Name\n");
        printf("Third Argument is Library File Name\n");
    }
    exit(0);
}

#define BOT 192
#define TOP 160
#define BIN 0

preparepic(char *picname)
{
   char c=0;
   int cflag=0;

   while(picname[c]!=0)
        {
            /* rudimentary check for type of screen that we are loading */
            if(picname[c]=='.')cflag=TOP;
            if(cflag==TOP)
            {
              if(picname[c]=='B'||picname[c]=='b')cflag=BOT;

             }
            if(cflag==BOT) {
			  if(picname[c]=='I'||picname[c]=='i')cflag=BIN;
			}
            if(picname[c]<' ')picname[c]=0;
            else c++;
        }

   piclode(picname,cflag);
}



piclode(name,type)
char *name;
int type;
{
   int fh,y,temp,packet=40,bos=type;
   char buffer[2];

   fh = open(name,O_RDONLY|O_BINARY); /* open a binary file */

   if (type == BIN) read(fh,(char *)&screenbuffer[0],0x2000);
   else {
   read(fh,buffer,2);
   for(y=0;y<bos;y++)
   {
     gethibase(y,&temp);
     read(fh,(char *)&screenbuffer[temp],packet);
     /* read each raster to the screenbuffer */
     }
   }
   close(fh);

}

/* provides base offset for hires scanlines         */
/* does not bother to check whether we are in range */
/* gets the address as quickly as possible          */
/* stays away from processor intensive mul and div  */

gethibase(currentline,currentbase)
int currentline;
int *currentbase;
{
  int ybase=0,z,a;

	if(currentline >63)
	   {
	   if (currentline < 128)
	       {
		ybase+=0x28;
		currentline-=64;
	       }
	   else
	       {
	       ybase+=0x50;
	       currentline-=128;
	       }
	    }

	    z=(currentline>>3);
	    a = (z<<7)|ybase;
	    *currentbase = (currentline - (z<<3))<<10 | a;
}

